﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FmScanTray : Form
    {

        public string TrayNo = "";
        public bool IsSpaceTray = false;
        private int ScanIndex = 0;
        DataTable cellDt = new DataTable();
        List<string> celllist = new List<string>();
        DataTable NGTrayDt;
        public FmScanTray()
        {
            InitializeComponent();
            cellDt.Columns.Add("chno");
            cellDt.Columns.Add("cellno");
        }

        private void tbxScan_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Alt) != 0 && (Control.ModifierKeys & Keys.Control) != 0 && (Control.ModifierKeys & Keys.Shift) != 0 && e.KeyCode == Keys.D0)
            {
                if (bttest.Visible == true)
                {
                    bttest.Visible = false;

                }
                else
                {
                    bttest.Visible = true;
                }
            }
            else
            {

                if (e.KeyCode != Keys.Enter)
                {
                    return;
                }
                string barcode = tbxScan.Text;
                tbxScan.Clear();
                if (ScanIndex > 16)
                {
                    tbxMessage.Text = "电芯数量已够16，无法再进行扫描，请进行下一步操作！";
                    return;
                }
                if (ScanIndex == 0)
                {
                    if (barcode.StartsWith("H") && barcode.Contains("-"))
                    {
                        labTrayNo.Text = barcode;
                        ScanIndex = 1;
                    }
                    else
                    {
                        tbxMessage.Text = "电芯数量已够16，无法再进行扫描，请进行下一步操作！";
                    }
                }
                else if (barcode.Length >= 18 && ScanIndex > 0)
                {
                    if (celllist.Contains(barcode))
                    {
                        tbxMessage.Text = "已扫描过该电芯！";
                        return;
                    }

                    DataRow dr = cellDt.NewRow();
                    dr["chno"] = ScanIndex.ToString();
                    dr["cellno"] = barcode;
                    cellDt.Rows.Add(dr);
                    celllist.Add(barcode);
                    ScanIndex++;
                    dgvBind();


                }
                else
                {
                    tbxMessage.Text = "扫描信息有误！请确认后重新扫描";
                    return;
                }

            }

            
        }

        private void tbxScan_Enter(object sender, EventArgs e)
        {
            tbxScan.BackColor = Color.Yellow;
        }

        private void tbxScan_Leave(object sender, EventArgs e)
        {
            tbxScan.Focus();    
        }

        private void FmScanTray_Load(object sender, EventArgs e)
        {
            tbxScan.Focus();
            tbxScan.Select();
        }
        private void InitAss()
        {
            ScanIndex = 1;
            labTrayNo.Text = string.Empty;
            cellDt.Rows.Clear();
            celllist.Clear();
        }

        private void FmScanTray_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Alt) != 0 && (Control.ModifierKeys & Keys.Control) != 0 && (Control.ModifierKeys & Keys.Shift) != 0 && e.KeyCode == Keys.D0)
            {
                if (bttest.Visible == true)
                {
                    bttest.Visible = false;
                }
                else
                {
                    bttest.Visible = true;

                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (cellDt.Rows.Count < 16)
            {
                //tbxMessage.Text = "托盘电池不足16只";
                //return;

                    //2017-4-28 10:15:50 因为BYD想手动生产只扫8只电池，所以自动补全cellDt到16只电池，缺少条码的位置补"",8只电池对应的顺序分别是2,4,6,8,10,12,14,16
                    if (cellDt.Rows.Count < 8)
                    {
                        tbxMessage.Text = "托盘电池不足8只";
                        return;
                    }
                //补全16只条码
                int gapno = 16 - cellDt.Rows.Count;
                int tempcelldtcount = cellDt.Rows.Count;
                for (int k = 0; k < gapno; k++)
                {
                    DataRow dr = cellDt.NewRow();
                    dr["chno"] = tempcelldtcount + k + 1;
                    dr["cellno"] = "";
                    cellDt.Rows.Add(dr);

                }

                for (int i = 8; i >= 0; i--)
                {
                    string cellno = "";
                    
                    int k = i * 2;
                    //找到2，4，6，8，10，12，14，16给他赋值
                    if (k % 2 == 0)
                    {
                        DataRow[] dr2 = cellDt.Select(" chno='" + (k-1) + "'");
                        if (dr2.Length > 0)
                        {
                            
                            if (dr2.Length > 0)
                            {
                                if (k == 2)
                                {
                                    cellno = dr2[0]["cellno"].ToString();
                                }
                                dr2[0]["cellno"] = "";
                            }
                        }

                        DataRow[] dr = cellDt.Select(" chno='" + (k/2)+"'");
                        if (dr.Length > 0)
                        {
                            if (k == 2)
                            {
                                DataRow[] dr1 = cellDt.Select(" chno='" + k + "'");
                                if (dr1.Length > 0)
                                {
                                    dr1[0]["cellno"] = cellno;
                                }
                            }
                            else
                            {
                                cellno = dr[0]["cellno"].ToString();
                                DataRow[] dr1 = cellDt.Select(" chno='" + k + "'");
                                if (dr1.Length > 0)
                                {
                                    dr1[0]["cellno"] = cellno;
                                }
                            }
                            
                            
                        }
                    }
                }
            }

            //以上部分到正式用时会注释掉，以上代码只使用与手动扫码，扫8只，对应着2，4，6，8，10，12，14，16通道


            TempDT.addcolumn();
            TempDT.dt.Clear();
            for (int i = 0; i < 16; i++)
            {
                
                
                DataRow dr1 =  TempDT.dt.NewRow();
                dr1["chno"] = cellDt.Rows[i]["chno"];
                dr1["cellno"] = cellDt.Rows[i]["cellno"];
                TempDT.dt.Rows.Add(dr1);
            }
                InitAss();
            dgvBind();
            this.Close();
            
        }

        private void dgvBind()
        {
            //dgvCellInfo.DataSource = cellDt;
            this.Invoke(new Action(() =>
                    {
                        dgvCellInfo.DataSource = cellDt.Copy();
                    }));
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            InitAss();
            dgvBind();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cellDt.Clear();
            for (int i = 1; i <= 16; i++)
            {
                
                DataRow dr = cellDt.NewRow();
                dr["chno"] = i;
                //DataRow[] dr1 = dttemp.Select("chno='"+i+"'");
                //dr["cellno"] = dr1[0]["cellno"];
                dr["cellno"] = "HQI19C15GRNS0122" + i.ToString().PadLeft(2, '0');
                cellDt.Rows.Add(dr);
            }
        }
    }
}
